// pages/love/love.js
const db = wx.cloud.database(); //初始化数据库
import {
  getTimeNow
} from '../../utils/util.js';
let page = 0
Page({

  /**
   * 页面的初始数据
   */
  data: {
    a:'',
    PL: '',
    userInfo: '',
    lovelist: [],
    lovesrc: [],
    showIndex: -1,
    placeholder: '请输入内容',
    showinput: false,
    PLs: [],
    index: '',
    showdelete: false,

  },

  // 打开地图 
  openLocation(e) {
    console.log('地图', e.currentTarget.dataset.index)
    const that = this;
    wx.openLocation({
      latitude: that.data.lovelist[e.currentTarget.dataset.index].latitude,
      longitude: that.data.lovelist[e.currentTarget.dataset.index].longitude,
    })

  },
  // 获取评论 输入框 内容
  getsay(e) {
    //console.log(e.detail.value)
    this.setData({
      PL: e.detail.value,
    })
  },
  // 用户点赞 头像，昵称 入数据库
  sendDZ(e) {
    //const that = this;    /* 倒序查询  ->  最新 */
    db.collection("love").orderBy('createTime', 'desc').where({
        _id: wx.getStorageSync('lovePL')
      })
      .get()
      .then(res => {
        if (res.data[0].bbb) {
          console.log("DZ为", res.data[0].bbb)
          console.log("DZ222为", res.data[0].bbb.length)
          for (var i = 0; i < res.data[0].bbb.length; i++) {
            console.log(res.data[0].bbb[i].openid)
            if (wx.getStorageSync('openid') == res.data[0].bbb[i].openid) {
              wx.showToast({
                title: '你已点赞',
                icon: 'none'
              })
            } else {
              this.reply(e)
              const sendtime = getTimeNow()
              wx.cloud.callFunction({
                name: 'getDZ',
                data: {
                  _id: wx.getStorageSync('lovePL'),
                  sendtime: sendtime,
                  saynickName: wx.getStorageSync('key').nickName,
                  openid: wx.getStorageSync('openid')
                },
                success: (res) => {
                  wx.showToast({
                    icon: "none",
                    title: '点赞成功',
                  })
                  //this.onLoad()
                  this.onShow()
                },
              })
            }

          }

        } else {
          this.reply(e)
          const sendtime = getTimeNow()
          wx.cloud.callFunction({
            name: 'getDZ',
            data: {
              _id: wx.getStorageSync('lovePL'),
              sendtime: sendtime,
              saynickName: wx.getStorageSync('key').nickName,
              openid: wx.getStorageSync('openid')
            },
            success: (res) => {
              wx.showToast({
                icon: "none",
                title: '点赞成功',
              })
              //this.onLoad()
              this.onShow()
            },
          })
        }



      })

    /*  this.reply(e)
      const sendtime = getTimeNow()
      wx.cloud.callFunction({
        name: 'getDZ',
        data: {
          _id: wx.getStorageSync('lovePL'),
          sendtime: sendtime,
          saynickName: wx.getStorageSync('key').nickName,
          openid: wx.getStorageSync('openid')
        },
        success: (res) => {
          wx.showToast({
            icon: "none",
            title: '点赞成功',
          })
          //this.onLoad()
          this.onShow()
        },
      }) */
  },

  // 点击评论
  sendPL() {
    console.log("点击了评论")
    this.setData({
      showinput: true
    })
  },
  // 点击省略号或块 评论
  reply(e) {
    console.log("77777", e)
    const id = e.currentTarget.dataset.index;
    wx.setStorageSync('lovePL', this.data.lovelist[id]._id)
    //this.setData({
      //index: id,
      //showinput: false 
    //})
  },

  //回复评论
  replytext(e) {
    console.log("88888", e)
    //console.log("评论的e为", e)
    //console.log(e.currentTarget.dataset.index)
    //console.log(e.currentTarget.dataset.name)
    //const that = this
    //const id = e.currentTarget.dataset.index; //点击评论的 id
    const name = e.currentTarget.dataset.name; //点击评论的 name
    //wx.setStorageSync('lovePL', that.data.lovelist[id]._id ) // 缓存数据库中的 id
    this.setData({
      placeholder: '回复' + name + '：',
      //index: id,
      showinput: true
    })

  },

  // 提交评论内容 入数据库
  sendPLS() {
    const that = this.data;
    //判断评论，不能为空
    if (!that.PL) {
      wx.showToast({
        icon: 'none',
        title: '评论信息不能为空 ！',
      })
      return;
    }
    const PL = that.PL
    if (that.placeholder === '请输入内容') {
      this.setData({
        placeholder: ''
      })
    }
    const placeholder = that.placeholder
    const sendtime = getTimeNow()
    //const _id = this.data.lovelist[this.data.index]._id //给 id 
    //wx.setStorageSync('lovePL', _id)
    wx.cloud.callFunction({
      name: 'getlovePL',
      data: {
        _id: wx.getStorageSync('lovePL'),
        PLs: placeholder + PL,
        sendtime: sendtime,
        sayavatarUrl: wx.getStorageSync('key').avatarUrl,
        saynickName: wx.getStorageSync('key').nickName
      },
      success: (res) => {
        //this.onShow()  
        this.setData({
          PL: '', // 清空输入框的内容
          placeholder: '请输入内容', // 恢复提示
          showinput: false, // 隐藏底部输入框
        })
        wx.showToast({
          icon: "none",
          title: '评论成功',
        })
        //this.onLoad()
        this.onShow()
      },
    })

  },

  showCiclk(e) {
    const that = this
    //console.log('eeeee',e)
    var index = e.currentTarget.dataset.index
    console.log("77777", index)
    if (that.data.showIndex == index) {
      that.setData({
        showIndex: -1
      })
    } else {
      that.setData({
        showIndex: index
      })
    }
  },
  // 点击任何 地方 隐藏输入框
  bindtouchstart() {
    this.setData({
      showinput: false,
      showIndex: -1
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
     // 过审核
     const that = this
     db.collection("a").get()
       .then(res => {
           console.log("11111111",res.data[0].a)
         that.setData({
           a: res.data[0].a,
         })
       })
    wx.showLoading({
      title: '加载中...',
    })
    const user = wx.getStorageSync('key')
    this.setData({
      userInfo: user,
    })

    setTimeout(() => {
      wx.hideLoading()
    }, 800)

    /*     const tha
    t = this
        db.collection("love").orderBy('createTime', 'desc')
          .get()
          .then(res => {
            console.log("取出来的数据", res.data)
            that.setData({
              lovelist: res.data,
            })
          }) */
    //this.getlovelist()








  },
  // 获取 数据  分页数据查询
  /*   getlovelist() {
      const that = this
      db.collection("love").orderBy('createTime', 'desc').skip(this.data.lovelist.length)
        .get({
          success(res) {
            console.log(res.data.length)
            that.setData({
              lovelist: that.data.lovelist.concat(res.data)
            })
            if (res.data.length != 0) {
              that.getlovelist()
            } else {
              wx.showToast({
                title: '暂无更多数据....',
                icon: 'none'
              })
            }
          }
        })


    }, */


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    wx.cloud.callFunction({
      name: 'getAll',
      success(res) {
        console.log("all", res)
        that.setData({
          lovelist: res.result
        })
        wx.setStorageSync('DZ', res.result.length)

      }
    })
  },

          /* for (var i = 0; i < res.result.length; i++) { */
        //console.log(that.data.lovelist[i]._openid)
        /*           if (that.data.lovelist[i]._openid == wx.getStorageSync('_openid')) {
                    that.data({
                      showdelete: true
                    })
                  } */
        // }
        //console.log('999', res.result[res.result.length])

  // 点击发布跳转
  send() {
    wx.navigateTo({
      url: '/pages/sendlove/sendlove',
    })

  },

  // 点击预览图片
  look(e) {
    console.log(e)
    console.log(e.currentTarget.dataset.src)
    wx.previewImage({
      //current: e.currentTarget.dataset.src,
      urls: e.currentTarget.dataset.src.lovesrc,
    })
  },

  // 点击预览顶部图片
  looktop() {
    wx.previewImage({
      //current: e.currentTarget.dataset.src,
      urls: ['cloud://cloud1-7g1a0u3je2bf6f8d.636c-cloud1-7g1a0u3je2bf6f8d-1314056523/0ebcc766dd20be27e8ce6ee7dfdf643.jpg'],
    })
  },
  // 解决：底部input，键盘弹起上移
  // 输入聚焦
  foucus: function (e) {
    var that = this;
    that.setData({
      inputBottom: e.detail.height
    })
  },
  //失去聚焦
  blur: function (e) {
    var that = this;
    that.setData({
      inputBottom: 0
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showToast({
      title: '页面刷新中...',
      icon: "none"
    })
    this.onShow()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showToast({
      title: '暂无更多数据啦~~~',
      icon: "none"
    })



  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /* 返回 */
  return () {
/*     wx.navigateTo({
      url: '/pages/index/index',
    }) */
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  // 删除发布的 朋友圈
  delete() {
    const that = this;
    wx.showModal({
      confirmColor: '#f35e63',
      content: '删除该朋友圈？',
      success(e) {
        if (e.confirm) {
          db.collection("love").orderBy('createTime', 'desc').where({
              _id: wx.getStorageSync('lovePL')
            }).get()
            .then(res => {
              console.log('147', res.data[0]._openid)
              if (res.data[0]._openid == wx.getStorageSync('openid')) {
                db.collection("love").orderBy('createTime', 'desc').where({
                    _id: wx.getStorageSync('lovePL')
                  })
                  .remove()
                  .then(res => {
                    wx.showToast({
                      title: '删除成功',
                    })
                    that.onShow()
                  })
              }
              else {
                wx.showToast({
                  title: '不要删除他人动态喔^_^',
                  icon: "none"
                })
              }
            })
        } else if (e.cancel) {
          console.log('用户点击取消')
        }  
        /*         if (res.confirm && res._openid == wx.getStorageSync('openid')) {
                  db.collection("love").orderBy('createTime', 'desc').where({
                      _id: wx.getStorageSync('lovePL')
                    })
                    .remove()
                    .then(res => {
                        wx.showToast({
                          title: '删除成功',
                        })
                        that.onShow()
                    })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }else if( res.cancel && res._openid != wx.getStorageSync('openid')) {
                  wx.showToast({
                    title: '不要删除他人动态喔^_^',
                    icon: "none"
                  })
                } */
      }
    })
  },

})